var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));//模板存放目录
app.set('view engine', 'ejs');//声明使用ejs引擎

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));//将请求显示到控制台中
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));//声明 img css js存放的目录（静态资源）
app.use(cookieParser('Wilson'));
app.use(session({ secret: 'wilson'}));
//保存用户信息 等其他全局信息
app.use(function(req,res,next){
    if(req.cookies["islogin"])
    {
        req.session.islogin = req.cookies.islogin["userName"];
        req.session.isAdmin = req.cookies.islogin["isAdmin"];
    }
    if(req.session.islogin)
    {
        res.locals.islogin = req.session.islogin;
        res.locals.isAdmin = req.session.isAdmin;
    }
    next();
});
var index = require('./routes/index'),
    login = require('./routes/login'),
    admin = require('./routes/admin'),
    article = require('./routes/article'),
    ajax = require('./models/ajax');
app.use('/',index );
app.use('/login',login );
app.use('/admin',admin);
app.use('/article',article);
app.use('/api',ajax);

module.exports = app;